package com.nvn.washiodriver.views;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.nvn.washiodriver.R;

public class CustomDialog extends Dialog {
	private List<Object> myArrayObject;
	private ListView myListView;
	private String title;
	public OnClickItem OnClickItem;
	private int itemSelect;

	public CustomDialog(Context context, List<Object> objects,
			String title, int itemSelect) {
		super(context);
		myArrayObject = objects;
		this.itemSelect = itemSelect;
		this.title = title;

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setLayout(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		this.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		setContentView(R.layout.layout_listview_dialog);
		init();
	}

	public void init() {
		myListView = (ListView) findViewById(R.id.lv_dialog);
		addHeader();
		myListView.setAdapter(new ArrayAdapter<Object>(getContext(),
				R.layout.item_dialog, myArrayObject) {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				View view = convertView;
				TextView tv = null;
				if (view == null) {
					view = LayoutInflater.from(getContext()).inflate(
							R.layout.item_dialog, null);
				}
				tv = (TextView) view.findViewById(R.id.tv);
				tv.setText(myArrayObject.get(position).toString());
				if (position == itemSelect) {
					tv.setTextColor(Color.parseColor("#009bf5"));
				} else {
					tv.setTextColor(Color.parseColor("#858585"));
				}
				return view;
			}
		});
		if (itemSelect > -1) {
			myListView.setSelection(itemSelect + 1);
		}
		myListView
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						if (arg2 > 0 && CustomDialog.this.OnClickItem != null) {
							CustomDialog.this.OnClickItem.Click(arg2);
							TextView tv = (TextView) arg1.findViewById(R.id.tv);
							tv.setTextColor(Color.parseColor("#009bf5"));
							CustomDialog.this.dismiss();
						}
					}
				});
	}

	public void addHeader() {
		View view = LayoutInflater.from(getContext()).inflate(
				R.layout.item_dialog, null);
		TextView text = ((TextView) view.findViewById(R.id.tv));
		text.setText(title);
		text.setTextSize(24);
		text.setClickable(false);
		text.setTextColor(Color.parseColor("#a9a9a9"));
		myListView.addHeaderView(view);
	}

	public void setOnClickItem(OnClickItem onClickItem) {
		this.OnClickItem = onClickItem;
	}

	public void show() {
		super.show();
	}

	public static interface OnClickItem {
		public void Click(int position);
	}
}
